// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        user: null,
        token: null,
    },
    mutations: {
        setUser(state, user) {
            state.user = user;
        },
        setToken(state, token) {
            state.token = token;
        },
        logout(state) {
            state.user = null;
            state.token = null;
        }
    },
    actions: {
        updateInfo({ commit }, { user }){
            commit('setUser', user);
            localStorage.setItem("user", JSON.stringify(user));

        },
        loginSuccess({ commit }, { user, token }) {
            commit('setUser', user);
            commit('setToken', token);
            // 可以选择在这里也更新localStorage
            localStorage.setItem("user", JSON.stringify(user));
            localStorage.setItem("token", token);
        },
        logout({ commit }) {
            commit('logout');
            // 清除localStorage
            localStorage.removeItem("user");
            localStorage.removeItem("token");
            localStorage.removeItem("chatHistory");
        }
    }
});
